using System.ComponentModel.DataAnnotations;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Configuration.Models;

/// <summary>
/// 锁定配置
/// </summary>
public class LockConfiguration
{
    /// <summary>
    /// 是否启用锁定功能
    /// </summary>
    public bool Enabled { get; set; } = false;

    /// <summary>
    /// 默认锁定超时（毫秒）
    /// </summary>
    [Range(1000, 300000)]
    public int DefaultTimeout { get; set; } = 30000;

    /// <summary>
    /// 最大重试次数
    /// </summary>
    [Range(0, 10)]
    public int MaxRetries { get; set; } = 3;

    /// <summary>
    /// 重试间隔（毫秒）
    /// </summary>
    [Range(100, 5000)]
    public int RetryInterval { get; set; } = 500;

    /// <summary>
    /// 是否使用分布式锁
    /// </summary>
    public bool UseDistributedLock { get; set; } = false;

    /// <summary>
    /// Redis 连接字符串（用于分布式锁）
    /// </summary>
    public string RedisConnectionString { get; set; } = string.Empty;
}

/// <summary>
/// 端点速率限制规则
/// </summary>
public class EndpointRateRule
{
    [Required]
    public string Endpoint { get; set; } = string.Empty;

    [Range(1, int.MaxValue)]
    public int Limit { get; set; } = 10;

    [Range(1, 3600)]
    public int WindowSize { get; set; } = 60;

    public string Method { get; set; } = "*";
}

/// <summary>
/// JWT 配置（增强版，对应 CacheSettings:Security:Jwt）
/// </summary>
public class EnhancedJwtConfiguration
{
    [Required]
    [MinLength(64, ErrorMessage = "SecretKey must be at least 64 characters long")]
    public string SecretKey { get; set; } = string.Empty;

    [Required]
    [Url(ErrorMessage = "Issuer must be a valid URL or identifier")]
    public string Issuer { get; set; } = "JGSY.CMS.LowCode.Platform";

    [Required]
    public string Audience { get; set; } = "JGSY.CMS.LowCode.Platform.Client";

    [Range(5, 1440, ErrorMessage = "Token expiry must be between 5 minutes and 24 hours")]
    public int ExpiryMinutes { get; set; } = 60;

    /// <summary>
    /// 刷新令牌过期天数
    /// </summary>
    [Range(1, 90)]
    public int RefreshExpiryDays { get; set; } = 7;

    /// <summary>
    /// 是否验证发行者
    /// </summary>
    public bool ValidateIssuer { get; set; } = true;

    /// <summary>
    /// 是否验证受众
    /// </summary>
    public bool ValidateAudience { get; set; } = true;

    /// <summary>
    /// 是否验证生命周期
    /// </summary>
    public bool ValidateLifetime { get; set; } = true;

    /// <summary>
    /// 是否验证签名密钥
    /// </summary>
    public bool ValidateIssuerSigningKey { get; set; } = true;

    /// <summary>
    /// 时钟偏移容差（秒）
    /// </summary>
    [Range(0, 300)]
    public int ClockSkew { get; set; } = 5;
}

/// <summary>
/// 速率限制规则配置（对应 CacheSettings:Security:RateLimit）
/// </summary>
public class SecurityRateLimitConfiguration
{
    /// <summary>
    /// 是否启用速率限制
    /// </summary>
    public bool EnableRateLimit { get; set; } = true;

    /// <summary>
    /// 通用规则
    /// </summary>
    public List<GeneralRateLimitRule> GeneralRules { get; set; } = new();
}

/// <summary>
/// 通用速率限制规则
/// </summary>
public class GeneralRateLimitRule
{
    [Required]
    public string Endpoint { get; set; } = string.Empty;

    [Required]
    public string Period { get; set; } = "1m";

    [Range(1, int.MaxValue)]
    public int Limit { get; set; } = 100;
}
